﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using IFRS.DataAccess.DAL;
using IFRS.Models;
namespace IFRS.DataAccess.DAL
{
    public class SPR_TimeToRecovery
    {
        public DataTable SEARCH_ALL(string collateralType, Paging paging, bool IFRS_ALLBRANCH, string sIFRS_COST_CENTER, ref int iRecordCount)
        {
            DataTable dt = new DataTable();
            IFRS_Utility Utility = new IFRS_Utility();
            List<SqlParameter> lstParams = new List<SqlParameter>();
            try
            {
                lstParams.Add(new SqlParameter("@RecordCount", SqlDbType.Int));
                lstParams[0].Direction = ParameterDirection.Output;
                lstParams.Add(new SqlParameter("@PageIndex", paging.PageIndex));
                lstParams.Add(new SqlParameter("@PageSize", paging.PageSize));
                lstParams.Add(new SqlParameter("@Sorting", paging.Sorting));
                lstParams.Add(new SqlParameter("@isAllBranch", IFRS_ALLBRANCH));
                lstParams.Add(new SqlParameter("@sCostCenter", sIFRS_COST_CENTER));
                lstParams.Add(new SqlParameter("@CollateralType", collateralType.AsDBValue()));                

                dt = Utility.StoredProcDataTable("SPR_TimeToRecovery_SEARCHALL", lstParams.ToArray());

                iRecordCount = (int)lstParams[0].Value;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        public DataTable SEARCH_BYID(int TimeToRecoveryID)
        {
            DataTable dt = new DataTable();
            IFRS_Utility Utility = new IFRS_Utility();
            List<SqlParameter> lstParams = new List<SqlParameter>();
            try
            {
                lstParams.Add(new SqlParameter("@TimeToRecoveryID", TimeToRecoveryID.AsDBValue()));

                dt = Utility.StoredProcDataTable("SPR_TimeToRecovery_SEARCHBYID", lstParams.ToArray());

            }
            catch (Exception ex)
            {
                throw ex;
            }

            return dt;
        }

        public int INSERT(TimeToRecovery timeToRecovery)
        {

            int iNum = 0;
            IFRS_Utility Utility = new IFRS_Utility();
            List<SqlParameter> lstParams = new List<SqlParameter>();

            try
            {
                lstParams.Add(new SqlParameter("@TimeToRecoveryID", SqlDbType.Int));
                lstParams[0].Direction = ParameterDirection.Output;               
                lstParams.Add(new SqlParameter("@CollateralTypeID", timeToRecovery.CollateralTypeID.AsDBValue()));               
                lstParams.Add(new SqlParameter("@TimeToRecoveryValue", timeToRecovery.TimeToRecoveryValue.AsDBValue()));
                lstParams.Add(new SqlParameter("@CreatedDate", timeToRecovery.CreatedDate.AsDBValue()));
                lstParams.Add(new SqlParameter("@CreatedBy", timeToRecovery.CreatedBy.AsDBValue()));
                lstParams.Add(new SqlParameter("@CreatedCostCenter", timeToRecovery.CreatedCostCenter.AsDBValue()));
                lstParams.Add(new SqlParameter("@CreatedBranch", timeToRecovery.CreatedBranch.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedDate", timeToRecovery.UpdatedDate.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedBy", timeToRecovery.UpdatedBy.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedCostCenter", timeToRecovery.UpdatedCostCenter.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedBranch", timeToRecovery.UpdatedBranch.AsDBValue()));

                Utility.ExecuteStoredProc("SPR_TimeToRecovery_INSERT", lstParams.ToArray());

                iNum = (int)lstParams[0].Value;
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return iNum;
        }

        public int UPDATE(TimeToRecovery timeToRecovery)
        {


            int iNum = 0;
            IFRS_Utility Utility = new IFRS_Utility();
            List<SqlParameter> lstParams = new List<SqlParameter>();
            try
            {
                lstParams.Add(new SqlParameter("@TimeToRecoveryID", timeToRecovery.TimeToRecoveryID.AsDBValue()));                
                lstParams.Add(new SqlParameter("@CollateralTypeID", timeToRecovery.CollateralTypeID.AsDBValue()));                
                lstParams.Add(new SqlParameter("@TimeToRecoveryValue", timeToRecovery.TimeToRecoveryValue.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedDate", timeToRecovery.UpdatedDate.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedBy", timeToRecovery.UpdatedBy.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedCostCenter", timeToRecovery.UpdatedCostCenter.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedBranch", timeToRecovery.UpdatedBranch.AsDBValue()));

                iNum = (int)Utility.ExecuteStoredProc("SPR_TimeToRecovery_UPDATE", lstParams.ToArray());
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return iNum;
        }

        public int DELETE(TimeToRecovery timeToRecovery)
        {
            int iNum = 0;
            IFRS_Utility Utility = new IFRS_Utility();
            List<SqlParameter> lstParams = new List<SqlParameter>();
            try
            {
                lstParams.Add(new SqlParameter("@TimeToRecoveryID", timeToRecovery.TimeToRecoveryID.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedDate", timeToRecovery.UpdatedDate.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedBy", timeToRecovery.UpdatedBy.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedCostCenter", timeToRecovery.UpdatedCostCenter.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedBranch", timeToRecovery.UpdatedBranch.AsDBValue()));

                iNum = (int)Utility.ExecuteStoredProc("SPR_TimeToRecovery_DELETE", lstParams.ToArray());
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return iNum;
        }
    }
}
